package cn.liangcaomei.educms.service.impl;

import cn.liangcaomei.educms.entity.CrmBanner;
import cn.liangcaomei.educms.mapper.CrmBannerMapper;
import cn.liangcaomei.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author LiuJie
 * @since 2024-02-04
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    //1 分页查询
    @Override
    public Page<CrmBanner> fetchAllBanner(long page, long limit) {
        Page<CrmBanner> bannerPage = new Page<>(page, limit);
        baseMapper.selectPage(bannerPage, null);
        return bannerPage;
    }

    @Override
    public boolean addBanner(CrmBanner crmBanner) {
        int result = baseMapper.insert(crmBanner);
        return result > 0;
    }

    //3 修改Banner
    @Override
    public boolean updateBanner(String id, CrmBanner crmBanner) {
        CrmBanner bannerById = baseMapper.selectById(id);
        if (bannerById != null) {
            int i = baseMapper.updateById(crmBanner);
            return i > 0;
        }
        return false;
    }

    //4 删除Banner
    @Override
    public boolean deleteBanner(String id) {
        CrmBanner result = baseMapper.selectById(id);
        if (result != null) {
            int i = baseMapper.deleteById(id);
            return i>0;
        }
        return false;
    }

    //Front-1 查询所有的Banner
    @Cacheable(key = "'selectIndexList'" ,value = "banner")
    @Override
    public List<CrmBanner> seleteAllBanner() {
        //根据id进行降序排序，取前面2笔记录
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");

        //last()方法，拼接sql语句
        wrapper.last("limit 2");
        List<CrmBanner> list = baseMapper.selectList(wrapper);
        return list;
    }
}
